package com.freud.logic.authorize.service;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import com.freud.framework.base.BaseService;
import com.freud.logic.authorize.beans.User;
import com.freud.logic.authorize.mapper.AuthorizeMapper;

public class AuthorizeService extends BaseService {

	@Autowired
	private AuthorizeMapper authorizeMapper;

	public static final String ROLE_USER = "ROLE_USER";
	public static final String ROLE_ADMIN = "ROLE_ADMIN";

	public User getUser(String username) {
		return authorizeMapper.get(null, username);
	}

	public User getUserById(int id) {
		return authorizeMapper.get(id, null);
	}

	public List<User> getAllUsers(RowBounds rb) {
		return authorizeMapper.getAll(rb, ROLE_USER);
	}

	public void insert(User user) {
		user.setRole(ROLE_USER);
		authorizeMapper.insert(user);
	}

	public void update(User user) {
		User exist = authorizeMapper.get(user.getId(), null);
		exist.setUsername(user.getUsername());
		authorizeMapper.update(exist);
	}

	public void deleteUser(int id) {
		authorizeMapper.delete(id);
	}
}
